Passed
Push — main ( d53ef8...c82fb0 )
by Andrii
01:47
created

index.ts ➔ main   C

Complexity

Conditions 10

Size

Total Lines 41
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 41
rs 5.9999
c 0
b 0
f 0
cc 10

How to fix   Complexity   

Complexity

Complex classes like index.ts ➔ main often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import { files } from "../files"
2
import {
3
  appenderSync,
4
  arr2line,
5
  createLineReader,
6
  templateLine
7
} from "../utils"
8
9
export {
10
  main,
11
  // varsMap
12
}
13
14
if (!module.parent)
15
  main()
16
17
async function main() {
18
  const templateRoot = `${__dirname}/template/`
19
  , outputRoot = `${__dirname}/output/`
20
21
  for (const id_ in files) {
22
    const {
23
      path,
24
    } = files[id_]
25
26
    if (!path)
27
      continue
28
29
    const append = appenderSync(`${outputRoot}${path}`)
30
31
    for await (const line of createLineReader(`${templateRoot}${path}`)) {
32
      const templ = templateLine<"id"|"value"|"prefix"|"postfix">(line)
33
34
      if (!templ) {
35
        await append(line, "\n")
36
        continue
37
      }
38
39
      const {
40
        indentation,
41
        id,
42
        value,
43
        prefix,
44
        postfix
45
      } = templ
46
      , valueStr = value ? `=${value}` : ""
47
48
      await append(
49
        arr2line(indentation, prefix, `ENV_${id}${valueStr}`, postfix)
50
      )
51
52
      for (const key in files) {
53
        if (key === id)
54
          continue
55
        await append(
56
          arr2line(indentation, prefix, `${id}_CATCH_${key}${value ? `=\${ENV_${key}}` : ""}`, postfix)
57
        )
58
      }
59
    }
60
  }
61
}
62